<?php

namespace app\admin\controller;

use think\Controller;
use app\common\controller\Admin;
use app\common\logic\SpecLogic;
use app\common\logic\TypeSpecLogic;
use app\common\logic\TypeLogic;
use app\common\model\Spec as SpecModel;
use app\common\model\Type;
class Spec extends Admin
{
    public function index()
    {
        
        $logic = new SpecLogic();
        $params = paramForm();
       
        if (\think\Request::instance()->isAjax()) {
            $params['paginate'] = true;
            $logic->setValue('order','type_id');
            $logic->setValue('with','val');//外键
            
            if (isset($params['title'])){
                $params['title'] = array('like','%'.$params['title'].'%');
            }
    
            if (isset($params['type_title'])){
                $params['type_title'] = array('like','%'.$params['type_title'].'%');
            }
            
            
            $lists = $logic->getList($params);
            foreach ($lists as $spec){
                $spec['vals'] = '';
                foreach ($spec['val'] as $key=>$val){
                    if($key>0){
                        $spec['vals'].=',';
                    }
                    $spec['vals'].=$val['name'];
                }
            }
            success($lists);
        } else {
            return $this->fetch();
        }
        
    }
    
    public function edit($id){
        $logic = new SpecLogic();
        $logic->setValue('with','val');
        $spec = $logic->findModel($id);
        $this->assign('model',$spec);
        return $this->fetch();
    }
    
    public function update($id){
        $params = paramForm();
        $spec = SpecModel::get($id);
        //清空原来的
        foreach ($spec->val as $val){
            $val->delete();
        }
        
        //保存标题
        $spec->title = $params['title'];
        $spec->isUpdate()->save();
        
        $valArr = [];
        foreach ($params['name'] as $val){
            $valArr[] = ['name'=>$val];
        }
        $rt = $spec->val()->saveAll($valArr);
        
        if($rt){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
        
        
        
    }
    

    
    public function save(){
        
        $error = 0;//记录错误几个
        $rule = [
            'is_attr|标记'=>'require',
            'title|属性名'=>'require',
            'typeid|子类目'=>'require',
            'title|属性名称'=>'require',
            'specs|属性可选值'=>'require'
        ];
       
        $params = paramForm();
        
        $this->validate($params,$rule);
    
        $logic = new SpecLogic();
        
        
        //拿到的参数是按顺序来的,不怕第二个缺了参数名,第三个缺了可选值的情况
        $post = [];
        foreach($params['title'] as $key=>$title){
            
            //初始化各种参数
            $valStr = $params['specs'][$key];
            $isAttr = $params['is_attr'][$key];
    
            //过滤,需要两个参数都为真
            if(!$title){
               
                continue;
            }
            
            $valArr = explode(',',$valStr);
            
            if(!is_array($valArr)){
                $error++;
                continue;
            }
            
            //先把spec入库,获取is_attr属性的方式有点奇怪
            $spec = ['type_id'=>$params['typeid'],'title'=>$title,'is_attr'=>$isAttr];

            $specid = $logic->add($spec);
           
            if(!$specid){
                $error++;//错误数量加一个
                continue;
            }
            
            //开始批量写入属性/规格的可选值
            $vals = [];
           foreach ($valArr as $val){
               $vals[] = ['spec_id'=>$specid,'name'=>$val];
           }
            
            $valIds = $logic->addVals($vals);
          
           if(!$valIds){
               $error++;
               continue;
           }
           
           
        }
    
        api(1,'填充完成,失败'.$error.'个','/spec/index',null);
        
    }
    
    
    public function getFoldList(){
        
        $len = paramForm('len')?paramForm('len'):1;
        $logic = new TypeLogic();
        $types = $logic->getFoldList($len);
        success($types);
        
    }
    
//    public function setSpecs(){
//
//        set_time_limit(0);
//        $error = [];
//        $logic = new SpecLogic();
//        $logic->setValue('size',null);
//        $logic->setValue('condition',['is_attr'=>1]);
//        $specs = $logic->prepareDataProvider();
//
//        foreach ($specs as $spec){
//            $spec->title = '价格';
//            $spec->save();
//        }
//
//    }
    
    
//    public function setSpecs(){
//
//        set_time_limit(0);
//        $error = [];
//        $logic = new SpecLogic();
//
////        $types = Type::where('level',3)->field('id,title')->limit(16)->select();
////        $types = Type::where('level',3)->field('id,title')->select();
//
//        $types = Type::where('id',2082)->field('id,title')->select();
//
//        foreach ($types as $type){
//            $valArr = explode(',','中国,越南,日本');
//            $spec = ['type_id'=>$type['id'],'type_title'=>$type['title'],'title'=>'产地','is_attr'=>1];
//
////            $valArr = explode(',','红色,白色,黑色');
////            $spec = ['type_id'=>$type['id'],'type_title'=>$type['title'],'title'=>'颜色','is_attr'=>0];
//
//            $specid = $logic->add($spec);
//
//            if(!$specid){
//                $error[]=$type;//错误数量加一个
//                continue;
//            }
//
//            //开始批量写入属性/规格的可选值
//            $vals = [];
//            foreach ($valArr as $val){
//                $vals[] = ['spec_id'=>$specid,'name'=>$val];
//            }
//
//            $valIds = $logic->addVals($vals);
//
//            if(!$valIds){
//                $error[]=$type;
//                continue;
//            }
//
//        }
//
//
//        echo '填充完成,失败'.count($error).'个';
//        echo '<pre>';
//        var_dump($error);die;
//
//    }
    
    
}
